
#include "Application.h"
#include "TitleView.h"
#include "BallView.h"
#include "MapEditView.h"
#include "TitleMenuItem.h"
#include "hgeUtil.h"

//
// Constructor
//

TitleView::TitleView()
{
	// Setup the bgQuad
	InitQuad(bgQuad, hge->Texture_Load("data/splatter.png"));
	SetQuadRect(bgQuad, 0, 0, ScreenWidth(), ScreenHeight());
	SetQuadTexCo(bgQuad);

	// Create the map view
	mapView = new MapEditView();

	// Create and initialize the GUI
	gui = new hgeGUI();

	hgeFont *fnt = GetLargeFont();
	gui->AddCtrl(new TitleMenuItem(1, fnt, menuSound, 10, 10, 0.0f, "Play"));
	gui->AddCtrl(new TitleMenuItem(2, fnt, menuSound, 10, 50, 0.1f, "Options"));
	gui->AddCtrl(new TitleMenuItem(3, fnt, menuSound, 10, 90, 0.2f, "Instructions"));
	gui->AddCtrl(new TitleMenuItem(4, fnt, menuSound, 10, 130, 0.3f, "Credits"));
	gui->AddCtrl(new TitleMenuItem(5, fnt, menuSound, 10, 170, 0.4f, "Exit"));

	gui->SetNavMode(HGEGUI_UPDOWN | HGEGUI_CYCLED);
	gui->SetCursor(GetCursorSprite());
}

//
// Destructor
//

TitleView::~TitleView()
{
	hge->Texture_Free(bgQuad.tex);

	delete mapView;
}

//
// Enter
//

void TitleView::Enter()
{
	gui->SetFocus(1);
	gui->Enter();
	lastID = -1;
}

//
// Process
//

View *TitleView::Process()
{
	// Get the time elapsed since last call of FrameFunc().
	// This will help us to synchronize on different
	// machines and video modes.
	float dt = hge->Timer_GetDelta();

	// We update the GUI and take an action if
	// one of the menu items was selected
	int id = gui->Update(dt);
	if(id == -1)
	{
		switch(lastID)
		{
			case 1:
				return mapView;
				gui->Leave();
				break;
			case 2:
			case 3:
			case 4:
				gui->SetFocus(1);
				gui->Enter();
				break;

			case 5: return 0;
			default: break;
		}
	}
	else if(id) { lastID = id; gui->Leave(); }

	return this;
}

//
// Render
//

void TitleView::Render()
{
	// Render quads here. This time just
	// one of them will serve our needs.
	hge->Gfx_RenderQuad(&bgQuad);

	// Render the gui
	gui->Render();
}